package com.ziyun.erp.web.modules.quoteConfig.controller;

import com.ziyun.erp.common.e.quote.QuoteConfigEnum;
import com.ziyun.erp.common.utils.JsonUtils;
import com.ziyun.erp.modules.quoteConfig.entity.QuoteConfigValueEntity;
import com.ziyun.erp.modules.quoteConfig.service.QuoteConfigValueService;
import io.renren.common.utils.DateUtils;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.sys.entity.SysUserEntity;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;


/**
 * 报价常量表
 *
 * @author  llc
 * @email 
 * @date 2018-09-06 10:14:25
 */
@RestController
@RequestMapping("/quoteConfig/quoteconfigvalue")
public class QuoteConfigValueController {
    @Autowired
    private QuoteConfigValueService quoteConfigValueService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("quoteConfig:quoteconfigvalue:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = quoteConfigValueService.queryPage(params);
        //存放枚举类到userdata
        Map<String, List> mapEnum = new HashMap<>();
        mapEnum.put("quoteConfigEnum", JsonUtils.enumToList(QuoteConfigEnum.class));
        return R.ok().put("page", page). put("userData", mapEnum);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("quoteConfig:quoteconfigvalue:info")
    public R info(@PathVariable("id") Integer id){
        QuoteConfigValueEntity quoteConfigValue = quoteConfigValueService.selectById(id);
        return R.ok().put("quoteConfigValue", quoteConfigValue);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("quoteConfig:quoteconfigvalue:save")
    public R save(@RequestBody QuoteConfigValueEntity quoteConfigValue){
        //默认创建人和更新人都是当前用户
        quoteConfigValue.setCreateUser( getCurrentUserName());
        quoteConfigValue.setUpdateUser(getCurrentUserName());
        quoteConfigValueService.insert(quoteConfigValue);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("quoteConfig:quoteconfigvalue:update")
    public R update(@RequestBody QuoteConfigValueEntity quoteConfigValue){
        ValidatorUtils.validateEntity(quoteConfigValue);
        quoteConfigValue.setUpdateUser(getCurrentUserName());//修改人
        quoteConfigValue.setUpdateDate(getCurrentTime());//修改时间
        quoteConfigValueService.updateAllColumnById(quoteConfigValue);//全部更新
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("quoteConfig:quoteconfigvalue:delete")
    public R delete(@RequestBody Integer[] ids){
        //伪批量删除
        quoteConfigValueService.deleteUpdateBatchIds(ids,getCurrentUserName());
        return R.ok();
    }

    /**
     * @author llc
     * @email
     * @date 2018-09-06 10:14:25
     * 根据id伪删除
     */
    @RequestMapping("/delete/{id}")
    @RequiresPermissions("quoteConfig:quoteconfigvalue:delete")
    public R deleteById(@PathVariable("id") Integer id ){
        Map<String, Object> map=new HashMap<>();
        map.put("id",id);
        map.put("update_user",getCurrentUserName());
        quoteConfigValueService.deleteUpdateById(map);
        return R.ok();
    }



    /**
     * @author llc
     * @email
     * @date 2018-09-06 10:14:25
     * 获取当前操作人员
     */
    private static String getCurrentUserName(){
        //获取创建人
        SysUserEntity user=(SysUserEntity)SecurityUtils.getSubject().getPrincipal();
        return user.getUsername();
    }

    /**
     * @author llc
     * @email
     * @date 2018-09-06 10:14:25
     * 获取当前操作时间
     */
    private static Date getCurrentTime(){
        //先将日期转换成字符串
        String stringDate = DateUtils.format(new Date(), DateUtils.DATE_TIME_PATTERN);
        return DateUtils.stringToDate(stringDate, DateUtils.DATE_TIME_PATTERN);
    }
}
